﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using FlexiGridHelper;
using System.Data;


namespace Golf.WebUI.HttpHandler.Commodities
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary> 
    public class Golf_BrandTypeList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            JavaScriptSerializer js = new JavaScriptSerializer();
            Golf.BLL.Golf_BrandType bll = new Golf.BLL.Golf_BrandType();
            List<Golf.Model.Golf_BrandType> list = new List<Golf.Model.Golf_BrandType>();
            //实例化页面信息类，获取页面传来的参数。该类是FlexiGridHelper中定义的类，封装了很多功能需要的数据
            PageView pageView = new PageView(context);
            //获取分页后的数据集
            DataSet ds = bll.GetList(pageView.PageSize, pageView.PageIndex, "");
            //构建表格
            DataTable dt = new DataTable();
            //构建列
            //注意：第一列必须是数据库的主键列 
            DataColumn dc0 = new DataColumn("Id", typeof(string));
            DataColumn dc1 = new DataColumn("TypeName", typeof(string));
            DataColumn dc2 = new DataColumn("AddAdminId", typeof(string));
            DataColumn dc3 = new DataColumn("AddTime", typeof(string));
            DataColumn dc4 = new DataColumn("IsCommend", typeof(string));
            DataColumn dc5 = new DataColumn("IsDisplay", typeof(string));
            //操作
            //把构建的列添加到数据表中
            dt.Columns.AddRange(new DataColumn[] { dc0, dc1, dc2, dc3, dc4, dc5 });//这里有个顺序的哟 
            list = bll.DataTableToList(ds.Tables[0]);//获取第一张表的数据  就是数据库中的数据
            //填充数据
            foreach (var item in list)
            {
                //新建行，注意是dt.NewRow();而不是new DataRow();
                DataRow dr = dt.NewRow();
                dr["ID"] = item.Id;
                dr["TypeName"] = item.TypeName;
                dr["AddAdminId"] = new Golf_ProductType().GetAdminName(item.AddAdminId);
                dr["AddTime"] = item.AddTime;
                dr["IsCommend"] = item.IsCommend;
                dr["IsDisplay"] = item.IsDisplay;
                dt.Rows.Add(dr);
            }
            //调用FlexiGrid数据格式转换方法
            context.Response.Write(js.Serialize(new GetDataHelper().GetFlexiGridData(pageView, dt, (int)ds.Tables[1].Rows[0][0])));//ds.Tables[1].Rows[0][0] 这里是数据库表的记录行数  是第二张表的 在dao类中的最后方法中
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
